﻿/*
 *  XTL - XNA Tool Library, a collection with usefull XNA classes
 * 
 *  Copyright (C) 2009 Sebastian Schäfer
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XTL.Display
{
    /// <summary>
    /// A simple FPS counter
    /// </summary>
    public class FpsCounter : HUDText
    {
        private float elapsed;
        private float frameRate;
        private float frames;
        
        /// <summary>
        /// Create a new instance
        /// </summary>
        /// <param name="game">[...]</param>
        public FpsCounter(Game game)
            : base(game)
        {
            elapsed = 0.0f;
            frameRate = 0.0f;
            frames = 0.0f;

            this.Color = Color.White;
            this.Position = new Vector2(10, 10);
        }

        /// <summary>
        /// Calculate frames and call the DrawMethod
        /// </summary>
        /// <param name="gameTime">elapsed gameTime</param>
        public override void Draw(GameTime gameTime)
        {
            elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (elapsed > 1.0f)
            {
                elapsed -= 1.0f;
                frameRate = frames;
                frames = 0;
            }
            else
            {
                frames += 1;
            }

            PrintText(Text + frameRate.ToString("0.00"));
        }
    }
}
